﻿namespace GreenIsomer.Web.Controls.JQuery {
	using System;
	using System.ComponentModel;
	using System.ComponentModel.Design.Serialization;
	using System.Globalization;
	using System.Reflection;
	using System.Collections;
	
	/// <summary>
	/// Provides conversions for the GreenIsomer.Web.Controls.JQuery.RelativeValue struct.
	/// </summary>
	public class RelativeValueConverter : TypeConverter {


		#region Constructors

		/// <summary>
		/// Initializes a new GreenIsomer.Web.Controls.JQuery.RelativeValueConverter class.
		/// </summary>
		public RelativeValueConverter() { }

		#endregion


		#region Methods: Capabilities

		/// <summary>
		/// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
		/// </summary>
		/// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
		/// <param name="sourceType">The type of the object to be converted.</param>
		/// <returns>Whether or not the conversion can be performed.</returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
			if(sourceType == typeof(string)) return true;
			return base.CanConvertFrom(context, sourceType);
		}

		/// <summary>
		/// Returns whether this converter can convert the object to the specified type, using the specified context.
		/// </summary>
		/// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
		/// <param name="destinationType">The target type of the object.</param>
		/// <returns>Whether or not the conversion can be performed.</returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
			if(destinationType == typeof(string)) return true;
			if(destinationType == typeof(InstanceDescriptor)) return true;
			return base.CanConvertTo(context, destinationType);
		}

		#endregion


		#region Methods: Perform tasks

		/// <summary>
		/// Converts the given object to the type of this converter, using the specified context and culture information.
		/// </summary>
		/// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
		/// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param>
		/// <param name="value">The object to be converted</param>
		/// <returns>The converted object.</returns>
		/// <exception cref="InvalidOperationException">Not a valid relative value.</exception>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
			if(value == null)
				throw new InvalidOperationException("Not a valid relative value.");

			string text = value as string;
			if(text != null) {
				if(text.Length == 0)
					throw new InvalidOperationException("Not a valid relative value.");

				bool relative = (text[0] == '+') || (text[0] == '-');

				int intValue;
				if(int.TryParse(text, out intValue))
					return new RelativeValue(relative, intValue);

				throw new InvalidOperationException("Not a valid relative value.");
			}

			return base.ConvertFrom(context, culture, value);
		}

		/// <summary>
		/// Converts the given object value to the specified type, using the specified context and culture information.
		/// </summary>
		/// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
		/// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param>
		/// <param name="value">The object to be converted</param>
		/// <param name="destinationType">The System.Type to convert the value parameter to.</param>
		/// <returns>The converted object.</returns>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {

			if(destinationType == typeof(string))
				return ((RelativeValue)value).ToString();

			if(destinationType == typeof(InstanceDescriptor)) {
				RelativeValue duration = (RelativeValue)value;

				return new InstanceDescriptor(
					typeof(RelativeValue).GetConstructor(new Type[] { typeof(bool), typeof(int) }),
					new object[] { duration.Relative, duration.Value });
			}

			return base.ConvertTo(context, culture, value, destinationType);
		}

		#endregion


	}

}